package com.payment.ktb.activity.main4;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.adapter.SettleBankCardRecordAdapter;
import com.payment.ktb.constants.Constants;
import com.payment.ktb.constants.ConstantsUser;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.model.SettleBankCard;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.Md5Utils;
import com.payment.ktb.utils.SharedPreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by syd on 2016/7/30.
 */
public class SettleBankCardManageHasCard extends BaseActivity implements AdapterView.OnItemClickListener{

    List<SettleBankCard> listData = new ArrayList<SettleBankCard>();
    SettleBankCardRecordAdapter adapter ;


    @BindView(R.id.tv_settlebankcardmanagehascard_bankNo)
    TextView tv_settlebankcardmanagehascard_bankNo;
    @BindView(R.id.tv_settlebankcardmanagehascard_bankName)
    TextView tv_settlebankcardmanagehascard_bankName;
    @BindView(R.id.tv_settlebankcardmanagehascard_bankInfoCnaps)
    TextView tv_settlebankcardmanagehascard_bankInfoCnaps;



    @BindView(R.id.lv_settlebankcardmanagehscard)
    ListView lv_settlebankcardmanagehscard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settlebankcardmanagehascard);
        ButterKnife.bind(this);
        initTitle("结算银行管理");
        lv_settlebankcardmanagehscard.setOnItemClickListener(this);
        getBankInfo();
        getBankInfoList();


    }


    @OnClick(R.id.btn_settlebankcardmanagehascard_changebankcard_)
    public void clickEvent(View view) {

        Intent intent = new Intent(mContext,AddSettleBankcardActivity.class);
        intent.putExtra("httpActionType","SettleBankCardManageHasCard");
        startActivity(intent);

    }


    /**
     * 商户结算银行卡信息
     */
    private void getBankInfo() {

        progressDialogUtils.showProgress();
        //构造传输参数
        String token = SharedPreferencesUtils.getString(ConstantsUser.userToken);
        //MD5（picNbr+key）
        String sign = Md5Utils.md5Hex(SharedPreferencesUtils.getString(ConstantsUser.userKey));

        //将参数放入map中
        HashMap<String, String> map = new HashMap<String,String>();
        map.put("token",token);
        map.put("sign",sign);

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.shopBankStateInfo, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {

                progressDialogUtils.dismissProgress();

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String code = jsonObject.optString("code");
                    if (Constants.NORMAL_CODE.equals(code)) {

                        tv_settlebankcardmanagehascard_bankNo.setText(jsonObject.optString("bankNo"));
                        tv_settlebankcardmanagehascard_bankName.setText(jsonObject.optString("bankName"));
                        tv_settlebankcardmanagehascard_bankInfoCnaps.setText(jsonObject.optString("bankInfoCnaps"));

                    } else {

                        AlertDialogUtils.showAlertDialog(mContext, jsonObject.optString("msg"));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {

                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.fail_to_connect_server));
            }
        });
    }







    /**
     * 商户结算银行卡变更记录
     */
    private void getBankInfoList() {

        progressDialogUtils.showProgress();
        //构造传输参数
        String token = SharedPreferencesUtils.getString(ConstantsUser.userToken);
        //MD5（picNbr+key）
        String sign = Md5Utils.md5Hex(SharedPreferencesUtils.getString(ConstantsUser.userKey));

        //将参数放入map中
        HashMap<String, String> map = new HashMap<String,String>();
        map.put("token",token);
        map.put("sign",sign);

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.shopBankStateList, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {

                progressDialogUtils.dismissProgress();

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String code = jsonObject.optString("code");
                    if (Constants.NORMAL_CODE.equals(code)) {

                        if (jsonObject.optString("taskInfoList") != null && jsonObject.optString("taskInfoList") != "") {

                            listData = new Gson().fromJson(jsonObject.getJSONArray("taskInfoList").toString(),new TypeToken<List<SettleBankCard>>(){}.getType());
                            adapter = new SettleBankCardRecordAdapter(listData,mContext);
                            lv_settlebankcardmanagehscard.setAdapter(adapter);
                        }

                    } else {

                        AlertDialogUtils.showAlertDialog(mContext, jsonObject.optString("msg"));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {

                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.fail_to_connect_server));
            }
        });
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent(mContext,BindCardDetailActivity.class);
        intent.putExtra("time",listData.get(position).getCreateTime());
        intent.putExtra("bankName",listData.get(position).getBankName());
        intent.putExtra("bankNo",listData.get(position).getBankNo());
        intent.putExtra("status",listData.get(position).getTaskStatus());
        intent.putExtra("memo",listData.get(position).getMemo());
        startActivity(intent);

    }
}
